package logic

import (
	"cartsrv/cartsrv"
	"cartsrv/internal/svc"
	"cartsrv/models/mysql"
	"context"
	"fmt"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsReleaseLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsReleaseLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsReleaseLogic {
	return &GoodsReleaseLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodsReleaseLogic) GoodsRelease(in *cartsrv.GoodsReleaseRequest) (*cartsrv.GoodsReleaseResponse, error) {
	// todo: add your logic here and delete this line
	goods := mysql.Goods{
		UserId:      in.UserId,
		GoodsTitle:  in.GoodsTitle,
		Price:       float64(in.Price),
		GoodsNumber: in.GoodsNumber,
		GoodsStock:  in.GoodsStock,
		Status:      int(in.Status),
		GoodsCover:  in.GoodsCover,
		GoodsStyle:  in.GoodsStyle,
	}
	mysql.Db.Where("goods_title ?", in.GoodsTitle).Find(&goods)
	if goods.ID != 0 {
		return nil, fmt.Errorf("商品发布错误")
	}
	mysql.Db.Debug().Create(&goods)

	return &cartsrv.GoodsReleaseResponse{Id: int64(goods.ID)}, nil
}
